define(function(){
	function regist(){
		function identity(){
			//验证码
			var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			var str1 = '';
			for(var i = 0;i < 4;i ++){
				var num = Math.floor(Math.random() * str.length);
				str1 += str.charAt(num);
			}
			$('.code').html(str1);
			return str1;
		}
		var str2 = identity();
		$('.reflash').click(function(){
			identity();
		})
		//添加正则事件
		//获取所有的span
		var spans = $('.txt');
		$('.txt').each(function(index,value){
			$(this).css('opacity','0');
		})
		//手机账号判断
		var onoff1 = false;
		$('#username').on({
				'focus' : function(){
					$(this).css('border-color','#c5c8c6');
				},
				'blur' : function(){
					var rx = RegExp();
					rx = /^1[34578]\d{9}$/;
					if($('#username').val()==''){
						spans[0].style.opacity = 1;
						spans[0].innerHTML = '手机作为登录账号，不能为空';
						$(this).css("border-color",'red');
						onoff1 = false;
					}else if(!(rx.test($('#username').val()))){
						spans[0].style.opacity = 1;
						spans[0].innerHTML = '手机格式输入不正确';
						$(this).css("border-color",'red');
						onoff1 = false;
					}else{
						spans[0].style.opacity = 0;
						onoff1 = true;
					}
				}
		})
		var onoff2 = false;
		//验证码判断
		$('#identity').on({
			'focus' : function(){
					$(this).css('border-color','#c5c8c6');
					spans[1].style.opacity = 0;
				},
			'blur' : function(){
				if($(this).val() == ''){
					spans[1].style.opacity = 1;
					spans[1].innerHTML = '验证码不能为空';
					$(this).parent().css("border-color",'red');
					onoff2 = false;
				}else if(!($(this).val().toLowerCase() == str2.toLowerCase())){
					spans[1].style.opacity = 1;
					spans[1].innerHTML = '验证码输入不正确';
					$(this).css("border-color",'red');
					str2 = identity();
					onoff2 = false;
				}else{
					spans[1].style.opacity = 0;
					$(this).parent().css("border-color",'#c5c8c6');
					onoff2 = true;
				}
			}
		})
		var onoff3 = false;
		//获取短讯验证码
		$('#mesCode').on({
			"focus": function(){
				spans[2].style.opacity = 0;
				$(this).parent().css('border-color',"#C5C8C6");
			},
			"blur" : function(){
				if($(this).val() ==''){
					spans[2].style.opacity = 1;
					spans[2].innerHTML = '验证码不能为空';
					$(this).parent().css("border-color",'red');
					onoff3 = false;
				}else{
					onoff3 = true;
				}
			}
		})
		$(".mesCodeBtn").click(function(){
			var num = 60;
			var timer = setInterval(function(){
				num --;
				$(".mesCodeBtn").html(num + 's');
//				console.log(num + "s");
				if(num <= 0){
					$(".mesCodeBtn").html('重新获取验证码');
					clearInterval(timer);
				}
			},1000)
		})
		var onoff4 = false;
		$('#pwd1').on({
			'focus' : function(){
				spans[3].style.opacity = 0;
				$(this).css('border-color','#C5C8C6');
			},
			'blur' : function(){
				var rx = new RegExp();
				rx = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,16}$/
				if($(this).val() == ''){
					spans[3].style.opacity = 1;
					spans[3].innerHTML = '密码不能为空';
					$(this).css('border-color','red');
					onoff4 = false;
				}else if($(this).val().length < 6){
					spans[3].style.opacity = 1;
					spans[3].innerHTML = '密码太短啦，不符合6个字符';
					onoff4 = false;
				}else if($(this).val().length > 16){
					spans[3].style.opacity = 1;
					spans[3].innerHTML = '密码太长啦，不能超过16个字符';
					onoff4 = false;
				}else if(!(rx.test($(this).val()))){
					spans[3].style.opacity = 1;
					spans[3].innerHTML = '请使用字母和数字组合的密码';
					onoff4 = false;
				}else{
					onoff4 = true;
				}
			}
		})
		var onoff5 = false;
		$('#pwd2').on({
			'focus' : function(){
				spans[4].style.opacity = 0;
				$(this).css('border-color','#C5C8C6');
			},
			"blur" : function(){
				if($(this).val() == ""){
					spans[4].style.opacity = 1;
					spans[4].innerHTML = '再次输入密码不能为空';
					onoff5 = false;
				}else if($(this).val() != $('#pwd1').val()){
					spans[4].style.opacity = 1;
					spans[4].innerHTML = '两次密码输入不相同';
					onoff5 = false;
				}else{
					onoff5 = true;
				}
			}
		})
		var onoff = false;
		$('#protocal').click(function(){
			if(!($(this).prop('checked'))){
				alert('请同意协议');
				onoff = false;
			}else{
				onoff = true;
			}
		})
		$('.submit').click(function(){
			//判断所有的input已经填写完毕并符合要求
			if(onoff == true&&onoff1 == true&&onoff2 == true&&onoff3 == true&&onoff4 == true&&onoff5 == true){
				//获取用户名
				var user = $("#username").val();
				//获取密码
				var pwd = $("#pwd1").val();
				var str = user + "/" +pwd;
				$.cookie("MCLuser",str);
				$(this).attr("href","login.html");
			}else{
				alert("请将内容填写正确");
			}
		})
	}
	return {
		regist : regist
	}	
})

